﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Cryptography_0912089_0912100.InputObject;

namespace Cryptography_0912089_0912100
{
    public partial class RijndaelCryptoForm : Form
    {
        private RijndaelCryptoHelper RijndaelHelper = new RijndaelCryptoHelper();
        private RijndaelInputObject RijndaelInput = new RijndaelInputObject();

        public RijndaelCryptoForm()
        {
            InitializeComponent();
        }

        private void buttonInput_Click(object sender, EventArgs e)
        {
            if (openFileDialogInput.ShowDialog() == DialogResult.OK)
            {
                textBoxInput.Text = openFileDialogInput.FileName;
            }
        }

        private void buttonOutput_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialogOutput.ShowDialog() == DialogResult.OK)
            {
                textBoxOutput.Text = folderBrowserDialogOutput.SelectedPath;
            }
        }

        private void radioButtonPKCS7_CheckedChanged(object sender, EventArgs e)
        {
            if (((RadioButton)sender).Checked)
            {
                RijndaelHelper.PaddingMode = RijndaelCryptoHelper.Padding.PKSC7;
            }
        }

        private void radioButtonX923_CheckedChanged(object sender, EventArgs e)
        {
            if (((RadioButton)sender).Checked)
            {
                RijndaelHelper.PaddingMode = RijndaelCryptoHelper.Padding.X923;
            }
        }

        private void radioButtonISO10126_CheckedChanged(object sender, EventArgs e)
        {
            if (((RadioButton)sender).Checked)
            {
                RijndaelHelper.PaddingMode = RijndaelCryptoHelper.Padding.ISO10126;
            }
        }

        private void radioButtonCBC_CheckedChanged(object sender, EventArgs e)
        {
            if (((RadioButton)sender).Checked)
            {
                RijndaelHelper.OperationMode = RijndaelCryptoHelper.Mode.CBC;
            }
        }

        private void radioButtonECB_CheckedChanged(object sender, EventArgs e)
        {
            if (((RadioButton)sender).Checked)
            {
                RijndaelHelper.OperationMode = RijndaelCryptoHelper.Mode.ECB;
            }
        }

        private void radioButtonOFB_CheckedChanged(object sender, EventArgs e)
        {
            if (((RadioButton)sender).Checked)
            {
                RijndaelHelper.OperationMode = RijndaelCryptoHelper.Mode.OFB;
            }
        }

        private void radioButtonCFB_CheckedChanged(object sender, EventArgs e)
        {
            if (((RadioButton)sender).Checked)
            {
                RijndaelHelper.OperationMode = RijndaelCryptoHelper.Mode.CFB;
            }
        }

        private void radioButtonCTS_CheckedChanged(object sender, EventArgs e)
        {
            if (((RadioButton)sender).Checked)
            {
                RijndaelHelper.OperationMode = RijndaelCryptoHelper.Mode.CTS;
            }
        }

        private void buttonEncrypt_Click(object sender, EventArgs e)
        {
            if (Check())
            {
                RijndaelInput.Input = textBoxInput.Text;
                RijndaelInput.Output = textBoxOutput.Text;
                RijndaelInput.Password = textBoxPassword.Text;
                RijndaelInput.IVString = textBoxIV.Text;
                RijndaelHelper.Encrypt(RijndaelInput);
            }
        }

        private void buttonDecrypt_Click(object sender, EventArgs e)
        {
            if (Check())
            {
                RijndaelInput.Input = textBoxInput.Text;
                RijndaelInput.Output = textBoxOutput.Text;
                RijndaelInput.Password = textBoxPassword.Text;
                RijndaelInput.IVString = textBoxIV.Text;
                RijndaelHelper.Decrypt(RijndaelInput);
            }
        }

        private bool Check()
        {
            if (textBoxInput.Text == "")
            {
                MessageBox.Show("No input file.",
                                "Error",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return false;
            }
            if (textBoxOutput.Text == "")
            {
                MessageBox.Show("No output directory.",
                                "Error",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return false;
            }
            if (textBoxPassword.Text.Length < 8)
            {
                MessageBox.Show("Password is invalid.",
                                "Error",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return false;
            }
            if (textBoxIV.Text.Length < 8)
            {
                MessageBox.Show("IV is invalid.",
                                "Error",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return false;
            }
            return true;
        }
    }
}
